package com.feihao.zhou.demo02_Map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class Demo02MapBianLi {
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(112,"小环");
        map.put(1003,"小欢");
        map.put(885,"小路");
        map.put(888,"小零零");

        //遍历：第一种：
        //将键获取到一个set集合：遍历set集合的key,通过map.get(key)获取value

        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            //获取value：
            String value = map.get(key);
            System.out.println(key + "=" + value);
        }

        System.out.println("======================");

        //遍历第二种：
        //获取键值对集合；EntrySet:再遍历EntrySet进行获取key+value
        Set<Map.Entry<Integer, String>> set = map.entrySet();
        Iterator<Map.Entry<Integer, String>> iterator = set.iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> s = iterator.next();
            System.out.println(s.getKey() + "=" + s.getValue());
        }

        System.out.println("======================");

        Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
        for (Map.Entry<Integer, String> entry : entrySet) {
            System.out.println(entry.getKey()+"="+ entry.getValue());
        }

        System.out.println("========调用函数式接口========");

        map.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer integer, String s) {
                System.out.println(integer + " = " + s);
            }
        });
    }
}
